package cn.lanqiao.exam;

import java.io.*;

public class CheckExam {
    //指定考试文件夹地址
    private String path = "/Users/David/Desktop/自动批卷";

    /**
     * 根据地址获得该地址下所有文件
     *
     * @param path
     * @return File[]
     * @throws Exception
     */
    public File[] getFileUrlList(String path) throws Exception {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length > 0) {
                return files;
            } else {
                throw new Exception("改地址内没有文件");
            }
        } else {
            throw new Exception("文件地址错误");
        }
    }

    /**
     * 读取文件中的内容，将其转换为字符串。
     *
     * @param file
     * @return
     */
    public String getStringFormFile(File file) {
        StringBuilder ret = new StringBuilder();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String s = null;
            while ((s = br.readLine()) != null) {
                ret.append(s);
                ret.append(",");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ret.toString();
    }


    public String[] getArrayFromString(String str) {
        return str.split(",");
    }

    /**
     * 提交答案和标准答案数组比对，返回正确个数。
     *
     * @param subAnswer
     * @param stanAnswer
     * @return
     */
    public int checkAnswer(String[] subAnswer, String[] stanAnswer) {
        int count = 0;
        //如果标准答案数量和提交答案数量不符，直接反馈错误。
        if (subAnswer.length != stanAnswer.length) {
            return -1;
        }
        //循环遍历得到正确答案数
        for (int i = 0; i < stanAnswer.length; i++) {
            if (subAnswer[i].equalsIgnoreCase(stanAnswer[i])) {
                count++;
            }
        }
        return count;
    }

    /**
     * 根据文件地址判断文件是否存在，存在则删除。
     * @param path
     */
    public void deleteFileByPath(String path){
        File file = new File(path);
        if(file.exists()){
            file.delete();
        }
    }

    public void run() {
        File[] answerFiles = null; // 学生提交答案文件数组
        int score = 0; //每道题得分
        String stanAnswerPath = path + File.separator + "标准答案.txt";
        String resultPath = path;
        try {
            //获得标准答案数组，和每道题的分数
            String[] stanAnswer = getArrayFromString(getStringFormFile(new File(stanAnswerPath)));
            score = 100 / stanAnswer.length;
            //获得学生提交答案所在文件夹，并且遍历文件夹拿到提交File数组
            File[] files = getFileUrlList(path);
            for (File file : files) {
                if (file.isDirectory()) {
                    answerFiles = getFileUrlList(path + File.separator + file.getName()); //获得考生提交答案file
                    resultPath += File.separator + file.getName() + "考试成绩.txt"; //生成输出地址
                    break;
                }
            }
            //判断结果路径是否有文档，有则删除
            deleteFileByPath(resultPath);
            //遍历学生提交答案文件，依次计算每个学生的分数，并输出分数。
            for (int i = 0; i < answerFiles.length; i++) {
                //获得学生提交答案
                String[] subAnswer = getArrayFromString(getStringFormFile(answerFiles[i]));
                // 通过比对得到正确答案个数
                int count = checkAnswer(subAnswer, stanAnswer);
                // 设置输出字符串 eg: 1 张三  90
                String outStr = (i + 1) + "\t" + answerFiles[i].getName().replace(".txt","") + "\t";
                outStr += (count == -1) ? "该学生答案有误！" : (count * score);
                outStr += "\r\n";
                // 向结果文件输出该学生成绩
                writeFile(resultPath, outStr);
                System.out.println(outStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public void writeFile(String path, String content) {

        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(new File(path), true));
            bufferedWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        new CheckExam().run();
    }

}
